$(function(){
    var scale = {

        // Configs
        _fixMode       : 'auto',      // Could be 'width' / 'height' / 'auto'
        _contentWidth  : 320,
        _contentHeight : 550,

        // Local vars
        _curRatio  : 1,
        _docWidth  : 0,
        _docHeight : 0,

        _els : [],

        _calcScaleRatio: function(){

            this._docWidth  = document.documentElement.clientWidth;
            this._docHeight = document.documentElement.clientHeight;

            var ratio = {
                width  : this._docWidth  / this._contentWidth,
                height : this._docHeight / this._contentHeight
            };

            this._curRatio = ratio[this._fixMode] ? ratio[this._fixMode] :
                    ratio.width > ratio.height ? ratio.height : ratio.width;

            return this;
        },

        _fixEl: function(){

            var ratio = this._curRatio;

            for (var l = this._els.length; l--;) {
                this._els[l].attr('style', '-webkit-transform:scale(' + ratio +
                        ',' + ratio + ');');
            }

            return this;
        },

        _bindResize: function() {

            var that = this;

            // Calculate ratio on 'resize' event
            $(window).bind('resize', function() {
                that._calcScaleRatio()._fixEl();
            });

            return this;
        },

        getScaleInfo: function() {
            return {
                ratio       : this._curRatio,
                docWidth    : this._docWidth,
                docHeight   : this._docHeight,
                slideWidth  : this._contentWidth,
                slideHeight : this._contentHeight
            };
        },

        addEl: function(elSeletor) {
            this._els.push($(elSeletor));
            return this._fixEl();
        },

        init: function() {
            return this._calcScaleRatio()._bindResize();
        }
    };
    /**
     * swipe 滑动手势封装
     */

    function Swipe(options) {
        this.init(options);
    }

    Swipe.prototype = {
        distance: 70,
        init: function(options) {

            var self = this;

            self.options = options;
            self._startPoint = [];
            self._endPoint = null;

            self._catching = false;

            // 事件绑定、
            $(window)
                .on('touchstart', function(event) {
                    // 开启记录状态
                    if (!self._catching) {
                        self._catching = true;
                        self._startPoint.push({
                            clientX :  event.touches[0].clientX,
                            clientY :  event.touches[0].clientY
                        });
                    }
                })
                .on('touchmove', function(event) {
                    event.preventDefault();

                    if (self._catching) {
                        self._endPoint = event.touches;
                    }
                })
                .on('touchend', function(event) {

                    self._calc();
                    // 关闭记录状态
                    self._catching = false;
                    self._startPoint = [];
                    self._endPoint = null;
                })
        },
        _calc: function() {
            var self = this;
            if (self._endPoint && self._startPoint) {
                var spx = self._startPoint[0].clientX,
                    spy = self._startPoint[0].clientY,

                    epx = self._endPoint[0].clientX,
                    epy = self._endPoint[0].clientY;

                if (Math.sqrt(Math.pow(epy - spy, 2) + Math.pow(epx - spx, 2)) > self.distance) {
                    var _a = Math.atan((epy - spy) / (epx - spx)) / Math.PI * 180;
                    // console.log(Math.atan((epy - spy)/(epx - spx)) /Math.PI * 180,epy-spy,epx-spx)

                    // swipeUp
                    if (epy - spy < 0 && (_a <= -60 || _a >= 60)) {
                        self.options.swipeUp && self.options.swipeUp()
                            // swipeDown
                    } else if (epy - spy > 0 && (_a <= -60 || _a >= 60)) {
                        self.options.swipeDown && self.options.swipeDown()
                            // swipeRight
                    } else if (epx - spx > 0 && _a <= 40 && _a >= -40) {
                        self.options.swipeRight && self.options.swipeRight()

                        // swipeLeft
                    } else if (epx - spx < 0 && _a <= 40 && _a >= -40) {
                        self.options.swipeLeft && self.options.swipeLeft()

                    }

                }
            }

        }
    }
    //scale.init().addEl('.m-screen-content');
    var curPage = 1;
    new Swipe({
        swipeDown:function(){
            
            if(curPage > 1){
                curPage --;
                $('body')[0].className = '';
                $('body').addClass('g-scroll-'+curPage);
            }

        },
        swipeUp:function(){
            if(curPage < 4){
                curPage ++;
                $('body')[0].className = '';
                $('body').addClass('g-scroll-'+curPage);
            }

        }
    });

    $('.g-screen .arrow').click(function(){
        if(curPage < 4){
            curPage ++;
            $('body')[0].className = '';
            $('body').addClass('g-scroll-'+curPage);
        }
    });

    if($(window).height() < 430){
        $('.g-main').addClass('screen-s')
    }



    // 横屏判断
    function handleOrientation(){
        if(window.orientation== 90|| window.orientation== -90){ //判断手机为横屏时
           $("#forhorview").show();
       }else{
            $("#forhorview").hide();
       }
    }

   window.addEventListener('deviceorientation', handleOrientation);
});